import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    LoginFormState state, Dispatch dispatch, ViewService viewService) {
  return SingleChildScrollView(
    child: buildForm(state, dispatch, viewService),
  );
}

Widget buildForm(
    LoginFormState state, Dispatch dispatch, ViewService viewService) {
  return FormBuilder(
    key: state.formKey,
    autovalidate: true,
    child: Column(
      children: <Widget>[
        FormBuilderTextField(
          controller: state.usernameController,
          attribute: 'username',
          decoration: InputDecoration(
            labelText: '用户名',
            helperText: '请输入用户名',
            icon: Icon(Icons.person),
          ),
          validators: [
            FormBuilderValidators.required(errorText: '用户名不能为空'),
          ],
        ),
        FormBuilderTextField(
          controller: state.passwordController,
          attribute: 'password',
          obscureText: true,
          decoration: InputDecoration(
            labelText: '密码',
            helperText: '请输入你的密码',
            icon: Icon(Icons.lock),
          ),
          validators: [
            FormBuilderValidators.required(errorText: '密码并不能为空'),
            FormBuilderValidators.minLength(6, errorText: '密码不能少于6位'),
          ],
        ),
        Padding(
          padding: const EdgeInsets.only(top: 28.0),
          child: Row(
            children: <Widget>[
              Expanded(
                child: RaisedButton(
                  padding: EdgeInsets.all(15.0),
                  child: Text("登录"),
                  color: Theme.of(viewService.context).primaryColor,
                  textColor: Colors.white,
                  onPressed: () {
                    if (!state.submitting &&
                        (state.formKey.currentState).validate()) {
                      dispatch(LoginFormActionCreator.onLogin());
                    }
                  },
                ),
              ),
            ],
          ),
        ),
      ],
    ),
  );
}
